// modified in home 1109
// modified in offi 1110

'use strict'
var g = {}
var asso = {}
var newConcept = function(name){
    if(!g[name]){
        g[name] = {
            name: name,
            visited: 0,
            zhuAsso:{}, //Asso bewteen concept and its zhu
            binAsso:{}, //Asso bewteen concept and its bin
            zhu:{}, // concept's zhu
            bin:{}, // concept's bin
            simpleAsso:[], //
        }
    }
    g[name].visited += 1
    return g[name]
}

// create asso of a concept. which could be accessed like conceptName.zhuAsso.zhuAssoName
var newConceptAsso = function(concept, zhuAsso, binAsso){
    if(zhuAsso && !concept.zhuAsso[zhuAsso.name]){
        concept.zhuAsso[zhuAsso.name] = {
            link: zhuAsso,
            concepts: []
        }
    }
    if(binAsso && !concept.binAsso[binAsso.name]){
        concept.binAsso[binAsso.name] = {
            link:binAsso, 
            concepts: []
        }
    }
}
// zhu is concept's zhu, bin is concept's bin.
// access bin like concept.bin.binName.link, 
// access zhu like concept.zhu.zhuName.link, 
var newConceptZB = function(concept, zhu, zhuAsso, bin, binAsso){
    newConceptAsso(concept, zhuAsso, binAsso)
    if(zhu && zhuAsso){
        concept.zhu[zhu.name] = {
            link: zhu,
            asso: zhuAsso,
        }
        concept.zhuAsso[zhuAsso.name].concepts.push(zhu)
    }
    if(bin && binAsso){
        concept.bin[bin.name] = {
            link: bin,
            asso: binAsso,
        }
        concept.binAsso[binAsso.name].concepts.push(bin)
    }

}
// create asso, which could be accessed like asso.actionName.zhuName.binName.link, or asso.actionName.zhuName.link
// and could be accessed reversely, first get bin ,then get zhu. like asso.actionName.__.binName.zhuName.link
var newAsso = function(zhu, action, bin){
    var zhuConcept = newConcept(zhu)
    var binConcept = newConcept(bin)
    if(!asso[action]){
        asso[action]= {
            name: action,
            visited: 0,
            aliases: [],
            abbrs: [], //abbreviation
            opposites:[], 
            __: {}, //reversed, bin.zhu.
        } 
    }
    asso[action].visited += 1

    if(!asso[action][zhuConcept.name]){
        asso[action][zhuConcept.name] = {
            link: zhuConcept,
        }
    }
    if(!asso[action][zhuConcept.name][binConcept.name]){
        asso[action][zhuConcept.name][binConcept.name] = {
            link: binConcept,
        }
    }

    //create reversed link
    if(!asso[action].__[binConcept.name]){
        asso[action].__[binConcept.name] = {
            link: binConcept,
        }
    }
    if(!asso[action].__[binConcept.name][zhuConcept.name]){
        asso[action].__[binConcept.name][zhuConcept.name] = {
            link: zhuConcept,
        }
    }

    newConceptZB(zhuConcept, null,null, binConcept, asso[action])
    newConceptZB(binConcept, zhuConcept, asso[action],null,null)

    return asso[action]
}


var newSimpleAsso = function(concept1, concept2){
    var c1 = newConcept(concept1)
    var c2 = newConcept(concept2)
    c1.simpleAsso.push(c2)
    c2.simpleAsso.push(c1)
}
var assoAlias = function(assoName,alias){
    asso[assoName].aliases.push(alias)
}
var assoAbbr = function(assoName,abbr){
    asso[assoName].abbrs.push(abbr)
}
var assoOpposite = function(assoName,opposite){
    asso[assoName].opposites.push(opposite)
}
newAsso('老王', '属于', '老师')
newAsso('老王', '属于', '物理老师')
newAsso('老王', '属于', '父亲')
newAsso('物理老师', '属于', '老师')
newSimpleAsso('物理','老师')
assoAlias('属于','是...之一')
assoAbbr('属于','是')
assoOpposite('属于','包含')
newAsso('Javascript', 'is_one_of', 'computer_language')
newAsso('Javascript', 'is_one_of', 'language')
newAsso('Javascript', 'is_one_of', 'high_level_language')
newAsso('Javascript', 'contains', 'function')
newAsso('Javascript', 'contains', 'variable')
console.log(g, asso)